package com.lagou.rpc.core;

import com.lagou.rpc.core.config.RpcConfig;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class RpcZookeeperClient {

    @Autowired
    private RpcConfig rpcConfig;

    private CuratorFramework client;

    @PostConstruct
    private void init(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
        client = CuratorFrameworkFactory.newClient(rpcConfig.getZookeeperUrl(), retryPolicy);
        client.start();
    }

    public CuratorFramework getClient(){
        return client;
    }


    public void close(){
        client.close();
    }

}
